package cn.iocoder.yudao.module.enterprise.controller.admin.controlplan;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.controlplan.vo.ControlPlanCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.controlplan.vo.ControlPlanPageReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.controlplan.vo.ControlPlanRespVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.controlplan.vo.ControlPlanUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.controlplan.ControlPlanConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.controlplan.ControlPlanDO;
import cn.iocoder.yudao.module.enterprise.service.controlplan.ControlPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name =  "管理后台 - 控制计划")
@RestController
@RequestMapping("/enterprise/control-plan")
@Validated
public class ControlPlanController {

    @Resource
    private ControlPlanService controlPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建控制计划")
    @PreAuthorize("@ss.hasPermission('enterprise:control-plan:create')")
    public CommonResult<Long> createControlPlan(@Valid @RequestBody ControlPlanCreateReqVO createReqVO) {
        return success(controlPlanService.createControlPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新控制计划")
    @PreAuthorize("@ss.hasPermission('enterprise:control-plan:update')")
    public CommonResult<Boolean> updateControlPlan(@Valid @RequestBody ControlPlanUpdateReqVO updateReqVO) {
        controlPlanService.updateControlPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除控制计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('enterprise:control-plan:delete')")
    public CommonResult<Boolean> deleteControlPlan(@RequestParam("id") Long id) {
        controlPlanService.deleteControlPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得控制计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('enterprise:control-plan:query')")
    public CommonResult<ControlPlanRespVO> getControlPlan(@RequestParam("id") Long id) {
        ControlPlanDO controlPlan = controlPlanService.getControlPlan(id);
        return success(ControlPlanConvert.INSTANCE.convert(controlPlan));
    }

    @GetMapping("/page")
    @Operation(summary = "获得控制计划分页")
    @PreAuthorize("@ss.hasPermission('enterprise:control-plan:query')")
    public CommonResult<PageResult<ControlPlanRespVO>> getControlPlanPage(@Valid ControlPlanPageReqVO pageVO) {
        PageResult<ControlPlanDO> pageResult = controlPlanService.getControlPlanPage(pageVO);
        return success(ControlPlanConvert.INSTANCE.convertPage(pageResult));
    }

}
